<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/9
 * Time: 15:51
 */

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Tool\Error;
use Illuminate\Http\Request;
use Qiniu\Storage\UploadManager;
use Qiniu\Auth;
use Intervention\Image\ImageManagerStatic as Image;

/**
 * Class UploadImgController
 * @package App\Http\Controllers
 * 七牛云上传文件
 */
class UploadImgController extends Controller
{
    private $arr = ['JPG', 'JPEG', 'PNG', 'GIF', 'jpg', 'jpeg', 'png', 'gif'];

    public function UploadPic(Request $request)
    {
        $file = $request->file('file');
        if ($file->isValid()) {
            $suffixname = $file->getClientOriginalExtension();
            if (in_array($suffixname, $this->arr)){
                $filePath =  $file->getRealPath();
                //上传七牛云的文件名称
                $nameHost = date('YmdHis',time()).uniqid() . '.' . $suffixname;
                $auth = new Auth(env('QINIU_ACCESSKEY'), env('QINIU_SECRETKEY'));
                $token = $auth->uploadToken(env('QINIU_BUCKET'));
                $uploadMgr = new UploadManager();
                $temp_arr = [
                    'color' => 1
                ];
                list($ret, $err) = $uploadMgr->putFile($token, $nameHost, $filePath,$temp_arr);
                if ($err !== null) {
                    //上传七牛云存储失败
                    return $this->error(Error::UPLOAD_ERROR);
                } else {
                    //上传成功 返回图片路径
                    $data = [
                        'preview_url' => env('QINIU_DOMAIN').$ret['key'],
                        'add_url'     => $ret['key']
                    ];
                    return $this->success('ok', $data);
                }
            }else{
                //文件格式错误
                return $this->error(Error::UPLOAD_SUFFIX_ERROR);
            }
        }else{
            //文件不合法
            return $this->error(Error::FILE_ERROR);
        }
    }


    /**
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     * 图片加水印
     */
    public function UploadFiles(Request $request)
    {
        $file = $request->file('file');
        $dst_path =  $file->getRealPath();
//        $dst_path = 'http://pd8frlxsk.bkt.clouddn.com/201808101506005b6d395877f55.jpg';
        //获取图片信息
        $info = getimagesize($dst_path);
        //创建图片的实例
        $dst = imagecreatefromstring(file_get_contents($dst_path));
        //打上文字
        $font = './fonts/simsun.ttc';//字体
        //颜色和透明度  最后一个参数值越大越透明
        $t_color = imagecolorallocatealpha($dst, 0, 0, 0, 50);
        //资源  字体大小 旋转角度 字体所在位置 X Y坐标 颜色 字体文件  字体内容
        imagefttext($dst, 30, 0, 50, 50, $t_color, $font, '这是文字水印');
        //输出图片
        list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
        header("Content-type:".$info['mime']);
        imagejpeg($dst, $dst_path);
//        switch ($dst_type) {
//            case 1://GIF
//                header('Content-Type: image/gif');
//                imagegif($dst);
//                break;
//            case 2://JPG
//                header('Content-Type: image/jpeg');
//                imagejpeg($dst);
//                break;
//            case 3://PNG
//                header('Content-Type: image/png');
//                imagepng($dst);
//                break;
//            default:
//                break;
//        }
        dd(imagedestroy($dst));
    }


    /**
     * @param $string
     * @return mixed|string
     * 汉字url  base64位安全编码
     */
    function urlsafe_b64encode($string) {
        $data = base64_encode($string);
        $data = str_replace(array('+','/','='),array('-','_',''),$data);
        return $data;
    }

}